//
//  GuideViewController.m
//  wifi4.2
//
//  Created by 郭四虎 on 15/11/13.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "GuideViewController.h"
#import "MyTabBarController.h"
#import "LoginViewController.h"
#import <CoreText/CoreText.h>

@interface GuideViewController ()<UIScrollViewDelegate>
{
    NSArray *arr;
    BOOL _isError;
    NSDictionary *_dic;
}
@property (strong, nonatomic) UIScrollView *sv;

@property (strong, nonatomic) UIPageControl *pageControl;

@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self prefersStatusBarHidden];
    [self createScrollView];
    [self createPageController];
    
    //开启推送
    [[NSUserDefaults standardUserDefaults]setObject:@(1) forKey:@"shouldRemote"];
    
    [MyCar isBus:^(bool isbus) {
        NSLog(@"qqqqq%d",isbus);
        if (isbus) {
            [[NSUserDefaults standardUserDefaults]setObject:@(1) forKey:@"isbus"];
            
        }else{
            [[NSUserDefaults standardUserDefaults]setObject:@(0) forKey:@"isbus"];
        }
        [[MyAd sharedMyad]getDataWithTag:@"loadpage##server" back:^(NSDictionary *dic) {
            _isError = NO;
            _dic = dic;
            
        } error:^(NSError *error,NSDictionary *di) {
            _isError = YES;
            
        }];
        
    }];
    
    
}

- (BOOL)prefersStatusBarHidden
{
    
    return YES;
}

-(void)createScrollView
{
    UIScrollView * scrollView = [[UIScrollView alloc] init];
    scrollView.delegate = self;
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    
    arr = @[@"图片.jpg",@"广告2.jpg"];
    
    for (int i = 0; i < arr.count; i ++) {
        UIImageView * imageView = [[UIImageView alloc] init];
        UIImage * image = [UIImage imageNamed:arr[i]];
        imageView.image = image;
        CGFloat imageViewX = WIDTH * i;
        imageView.frame = CGRectMake(imageViewX, 0, WIDTH, HEIGHT+1);
        
        [scrollView addSubview:imageView];
        
        if (i == arr.count-1) {
            [self setUpLastImageViewBtn:imageView];
        }
    }
    
    scrollView.contentSize = CGSizeMake(WIDTH * arr.count, 0);
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    //scrollView.bounces = NO;
    
}
-(void)setUpLastImageViewBtn:(UIImageView *)imageView
{
    // 让imageView能跟用户交互
    imageView.userInteractionEnabled = YES;
    
    // 添加开始按钮
    UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //startButton.layer.borderWidth = 2;
    //startButton.layer.borderColor = [UIColor redColor].CGColor;
    startButton.frame = CGRectMake(WIDTH*0.26, HEIGHT*0.85, 150, 50);
    startButton.center = CGPointMake(WIDTH*0.5, HEIGHT*0.88);
    //[startButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    //[startButton setTitle:@"立即体验>>" forState:UIControlStateNormal];
    [imageView addSubview:startButton];
    
}
-(void)start
{
    // 显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    // 切换窗口的根控制器
    
//    MyTabBarController *tab = [[MyTabBarController alloc]init];
//    self.view.window.rootViewController = tab;
    
    if (!_dic) {
        
        
        
        return;
    }
    
    MyTabBarController *tab = [[MyTabBarController alloc]initWithDic:_dic];
    //tab.dataDic = _dic;
    
    self.view.window.rootViewController = tab;
}




-(void)createPageController
{
    // 添加
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = arr.count;
    CGFloat centerX = self.view.frame.size.width * 0.5;
    CGFloat centerY = HEIGHT * 0.82;
    pageControl.center = CGPointMake(centerX, centerY);
    pageControl.bounds = CGRectMake(0, 0, 100, 30);
    pageControl.userInteractionEnabled = NO;
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 取出水平方向上滚动的距离
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 求出页码
    double pageDouble = offsetX / scrollView.frame.size.width;
    int pageInt = (int)(pageDouble + 0.5);
    self.pageControl.currentPage = pageInt;
    
    if (offsetX>WIDTH+50) {
        [self start];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
